package com.sdkj.service.goods.applygoods;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sdkj.dao.DaoSupport;
import com.sdkj.entity.Page;
import com.sdkj.util.PageData;
import com.sdkj.util.UuidUtil;


@Service("applygoodsdetailService")
public class ApplyGoodsDetailService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;
	
	/*
	* 新增
	*/
	public void save(PageData pd)throws Exception{
		dao.save("ApplyGoodsDetailMapper.save", pd);
	}
	
	/*
	* 删除
	*/
	public void delete(PageData pd)throws Exception{
		dao.delete("ApplyGoodsDetailMapper.delete", pd);
	}
	
	/*
	* 申请物品详情修改
	*/
	public void edit(PageData pd)throws Exception{
		PageData pdApply = new PageData();
		pdApply.put("apply_goods_id", pd.getString("apply_goods_id"));
		pdApply.put("apply_acount_price", pd.getString("apply_acount_price"));
		pdApply.put("remark", pd.getString("remark"));
		dao.update("ApplyGoodsMapper.edit", pdApply);
		String delsStr = pd.getString("dels");// 要删除的物品
		String saveStr = pd.getString("apply_kind_num");// 要增加的物品
		String idsStr = pd.getString("goods_id");// 要增加的id
		String[] dels = delsStr.split(",");
		for (int i = 0; i < dels.length; i++) {
			PageData pdDel = new PageData();
			pdDel.put("apply_goods_detail_id", dels[i]);
			dao.delete("ApplyGoodsDetailMapper.delete",pdDel);
		}
		if (saveStr != null && !"".equals(saveStr)) {
			String[] strs = saveStr.split(",");
			String[] ids = idsStr.split(",");
			// 打印纸-单价20.5-数量2-共41元
			for (int i = 0; i < strs.length; i++) {
				String[] params = strs[i].split("-");
				String name = params[0];
				String price = params[1]
						.substring(params[1].lastIndexOf("价") + 1);
				String num = params[2]
						.substring(params[2].lastIndexOf("量") + 1);
				String account = params[3].substring(
						params[3].lastIndexOf("共") + 1,
						params[3].lastIndexOf("元"));
				PageData pdDetail = new PageData();
				pdDetail.put("apply_goods_detail_id", UuidUtil.get32UUID());
				pdDetail.put("apply_goods_id", pd.getString("apply_goods_id"));
				pdDetail.put("goods_id", ids[i]);
				pdDetail.put("goods_name", name);
				pdDetail.put("goods_price", price);
				pdDetail.put("goods_num", num);
				pdDetail.put("goods_account", account);
				pdDetail.put("apply_remark", "");
				dao.delete("ApplyGoodsDetailMapper.save",pdDetail);
			}
		}
		
		
	}
	/*
	* 修改采购完成
	*/
	public void editEnd(PageData pd)throws Exception{
		dao.update("ApplyGoodsDetailMapper.editEnd", pd);
	}
	/*
	*列表
	*/
	public List<PageData> list(Page page)throws Exception{
		return (List<PageData>)dao.findForList("ApplyGoodsDetailMapper.datalistPage", page);
	}
	
	/*
	*列表(全部)
	*/
	public List<PageData> listAll(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("ApplyGoodsDetailMapper.listAll", pd);
	}
	
	/*
	* 通过id获取数据
	*/
	public PageData findById(PageData pd)throws Exception{
		return (PageData)dao.findForObject("ApplyGoodsDetailMapper.findById", pd);
	}
	
	/*
	* 批量删除
	*/
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		dao.delete("ApplyGoodsDetailMapper.deleteAll", ArrayDATA_IDS);
	}
	
}

